<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>POST JOB</title>

<link href="../css/post-style.css" rel='stylesheet' type='text/css'>
<link href="../jQueryAssets/jquery.ui.core.min.css" rel="stylesheet" type="text/css">
<link href="../jQueryAssets/jquery.ui.theme.min.css" rel="stylesheet" type="text/css">
<link href="../jQueryAssets/jquery.ui.datepicker.min.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="../tinymce/js/tinymce/tinymce.min.js"></script>
<script src="../jQueryAssets/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="../jQueryAssets/jquery-ui-1.9.2.datepicker.custom.min.js" type="text/javascript"></script>
<script type="text/javascript">
tinymce.init({	
 selector: "textarea",
plugins: [
            "advlist autolink autosave link image lists charmap print preview hr anchor pagebreak spellchecker",
            "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
            "table contextmenu directionality emoticons template textcolor paste fullpage textcolor"
    ],
 toolbar: "insertfile undo redo | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent",
  menubar: false,
    toolbar_items_size: 'small',
	
    height: "100",
    width:900,
 });
</script>


<?php

include('../php/db.php');

$db = new mysqli('localhost',$usercon,$passcon,'flss');
    

if ($_SESSION['login'] == NULL)
{
 header("Location: ../index.php");	
}
else
{
$login = $_SESSION['login'];

$userid = mysql_query("SELECT * FROM `flss`.`accounts` WHERE `user` = '$login'") or die(mysql_error());
while($rowshow = mysql_fetch_array($userid))
{
	$getid = $rowshow['account_id'];
}
$query = "SELECT * FROM Job_Category";
$result = $db->query($query);

while($row = $result->fetch_assoc()){
    $categories[] = array("id" => $row['Category_ID'], "val" => $row['Jobs_Name']);
}

  $query = "SELECT * FROM Job_Sub";
  $result = $db->query($query);

  while($row = $result->fetch_assoc()){
    $subcats[$row['Category_ID']][] = array("id" => $row['sub_no'], "val" => $row['sub_job']);
  }

$query2 = "SELECT * FROM `flss`.`educ_attainment`" or die(mysql_error());
$result2 = $db->query($query2);

while($row2 = $result2->fetch_assoc()){
    $educlvl[] = array("id" => $row2['educ_level'], "val" => $row2['descirption']);
}

$query3 = "SELECT * FROM `flss`.`year_experienced`" or die(mysql_error());
$result3 = $db->query($query3);

while($row3 = $result3->fetch_assoc()){
    $yrxp[] = array("id" => $row3['yr_ID'], "val" => $row3['ye_level']);
}
  $jsonYrexp = json_encode($yrxp);
  $jsonEduc = json_encode($educlvl);
  $jsonCats = json_encode($categories);
  $jsonSubCats = json_encode($subcats);




}
?>

 <script type='text/javascript'>
      <?php
        echo "var categories = $jsonCats; \n";
        echo "var subcats = $jsonSubCats; \n";
        echo "var educlvl = $jsonEduc; \n";
      	echo "var yrxp = $jsonYrexp; \n";
      
	  ?>
	        function loadset(){
			loadCategories();
			loadeduc();
			loadxp();	
			}
      function loadCategories(){
        var select = document.getElementById("categoriesSelect");
        select.onchange = updateSubCats;
		for(var i = 0; i < categories.length; i++){
          select.options[i] = new Option(categories[i].val,categories[i].id);          
        }
		loadeduc();
		loadxp();
      }
      function updateSubCats(){
        var catSelect = this;
        var catid = this.value;
        var subcatSelect = document.getElementById("subcatsSelect");
        //subcatSelect.options.length = 0; //delete all options if any present
        for(var i = 0; i < subcats[catid].length; i++){
          subcatSelect.options[i] = new Option(subcats[catid][i].val,subcats[catid][i].id);
        }
      }
	  function loadeduc(){
	  	var select = document.getElementById("educ");
        for(var i = 0; i < educlvl.length; i++){
          select.options[i] = new Option(educlvl[i].val,educlvl[i].id);          
        }
	  }
	  
	   function loadxp(){
		var select = document.getElementById("yrexp");
        for(var i = 0; i < yrxp.length; i++){
          select.options[i] = new Option(yrxp[i].val,yrxp[i].id);          
        }
	   }
	   
	   
	   function charLimit(limitField, limitNum) {
		  if (limitField.value.length > limitNum) {
		   limitField.value = limitField.value.substring(0, limitNum);}
		}
	 </script>


</head>


<body onload='loadset()'>


<?php
 include __DIR__ . "/../php/header.php";

?>

<div id="main" class="b-content">

	<header class="page-header">
     <h1 class="page-title"><font style="font-family:'Gadugi'"> Post A Job </font></h1>
    </header>
    <div id="primary" class="primary-content">
		<div id="content" class="primary-container" role="main">
			<article id="post-14" class="post-14 page type-page status-publish hentry">
				<div class="primary-entrycontent">
					<form method="post" id="submit-job-form" class="job-manager-form" enctype="multipart/form-data">
                    <fieldset class="fieldset-job_title">
                    <label for="job_title">Job Title</label>
                    <div class="field required-field">
                    <input type="text" class="input-text" name="jtitle" id="job_title" placeholder="" value="" onkeyup='charLimit(this, 200);' required>
                    </div>
                    </fieldset>
                    <fieldset class="fieldset-job_type">
                    <label for="job_type">Job Category</label>
                    <div class="field required-field">
                    <div class="select">
                    <select name="category" id="categoriesSelect" class="postform" style="border: 0px;" required>
                    </select>
                    </div>
                    </div>
                    </fieldset>
    					
                    <fieldset class="fieldset-job_type">
                    <label for="job_type">Job Sub-Category</label>
                    <div class="field required-field">
                    <div class="select">
                    <select id='subcatsSelect' name="subcatid" class="postform" style="border: 0px;" required>
                    	<option value="NULL"> Choose Sub Category </option>
                    </select></div>
                    </div>
                    </fieldset>
                    
                    <fieldset class="fieldset-basic-qualifications">
                    <label for="Basic-qualifications">Basic Qualifications:<br><br>Minimum Educational Attainment</label>
                    <div class="field required-field">
                    <div class="select">
                    <select name="educatt" id="educ" class="postform" style="border: 0px;"  required>
                         <option value="NULL"> Choose Minimum Educational Attainment </option>
                         </select>
                       </div>
                    </div>
                    </fieldset>
                    <fieldset class="fieldset-exp">
                    <label for="exp">Years of Experience Needed</label>
                    <div class="field required-field">
                    <div class="select">
                    <select name="expr" id="yrexp" class="postform" style="border: 0px;"  required>
                       			<option value="NULL" > Choose Years of Experience Needed </option>
                         </select>
                       </div>
                   </div>
                    </fieldset>
                    <fieldset class="fieldset-job_description">
                    <label for="job_description">Job Description</label>
                    	<div class="field required-field">
                     		<textarea name="description" required></textarea>
                    	</div>
                    </fieldset>
                    <fieldset class="fieldset-reqmts">
                    <label for="reqmts">Requirements</label>
                    	<div class="field required-field">
                     		<textarea name="requirements" required></textarea>
                    	</div>
                    </fieldset>
                    <fieldset class="fieldset-job_deadline">
                    <label for="job_deadline">Closing date <small>(optional)</small></label>
                    <div class="field">
                    <input type="text" id="Datepicker1" placeholder="yyyy-mm-dd" name="datedeadline">
					<script type="text/javascript">
					$(function() {
						var pickerOpts = {
								minDate: new Date(),
								maxDate: "+30",
								dateFormat: "yy-mm-dd"
							};
						$( "#Datepicker1" ).datepicker(pickerOpts); 
					});
					</script>
                    <small class="description">Deadline for new applicants. Default 1 Month before closing job</small>	</div>
                    </fieldset>
                    <fieldset class="fieldset-numapps">
                    <label for="numapps">Number Of Applicants Needed</label>
                    <div class="field required-field">
                    <input type="number" class="input-text" name="numapps" id="num_apps" placeholder="e.g 20" value="" maxlength="" required>
                    </div>
                    </fieldset>
                    <p>
                    <input type="submit" name="btnSubmit" onClick="javascript:tinyMCE.triggerSave();" class="btn_submit" value="Preview →">
                    </p>
                    </form>
              </div>
          </article>	
      </div>
    </div>
</div>
<?php

if(isset($_POST['btnSubmit']))

{
	if($_POST['jtitle'] != NULL)
	{
	$jtitle = $_POST['jtitle'];
	$jcategory = $_POST['category'];
	$jscategory = $_POST['subcatid'];
	$jeducatt = $_POST['educatt'];
	$jexpr = $_POST['expr'];
	$jdescription = $_POST['description'];
	$jrequirements = $_POST['requirements'];
	if(!$_POST['datedeadline'])
	{
		$jdate = Date("Y-m-d", strtotime("+1 Month"));
	}
	else
	{$jdate = $_POST['datedeadline'];}
	$japp = $_POST['numapps'];
	$refid = uniqid();
	$today = date('y-m-d');
  
	mysql_query("INSERT INTO `flss`.`jobs_temp` (`Jobs_ID`, `Jobs_title`, `Jobs_Category`, `Jobs_Subcategory`, `Jobs_MinEd`, `Jobs_YoE`, `Jobs_Description`, `Jobs_Requirements`, `Jobs_ClosingDate`, `Jobs_AppNeeded`, `Employer_ID`, `refid`, `date_posted`) 
	VALUES ('', '$jtitle', '$jcategory', '$jscategory', '$jeducatt', '$jexpr', '$jdescription', '$jrequirements', '$jdate', '$japp', '$getid', '$refid', '$today')") or die(mysql_error());
	
echo "<script>

var mywindow = window.open('previewjob.php?refid=$refid','popupwindow','left=200,top=50,width=1000,height=600,scrollbars=yes,menu=0,status=0');

</script>";
	
}
}

?>

</body>
</html>
